package cn.t16.ms.product;

import cn.t16.common.Result;
import cn.t16.ms.feign.order.OrderClient;
import cn.t16.ms.feign.order.OrderVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
public class UserController {
    @Resource
    private UserService userService;

    @Resource
    private OrderClient orderClient;

    @GetMapping("/myorders/{userId}")
    public Result getMyOrder3(@PathVariable Integer userId) {
        List<OrderVO> list  = orderClient.getByUserId(userId);
        return Result.returnResult(200,"success", list);
    }
    @GetMapping("/orders/{orderId}")
    public Result getOrder(@PathVariable Integer orderId) {
        Result result = orderClient.get(orderId);
        return result;
    }

    @GetMapping("/users/{id}")
    public Result get(@PathVariable Integer id) {
        User user =  userService.get(id);
        return Result.returnResult(200,"success",user);
    }
    @PostMapping("/users2")
    public Result save2(User user, MultipartFile idPicPath) {
        if(!idPicPath.isEmpty()) {
            String path = "c:/";
            try {
                idPicPath.transferTo(new File(path, idPicPath.getOriginalFilename()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        userService.save(user);
        return Result.returnResult(200,"success",user);
    }
    @PostMapping("/users")
    public Result save(@RequestBody User user) {
        userService.save(user);
        return Result.returnResult(200,"success",user);
    }

    @GetMapping("/users")
    public Result getAll() {
        List<User> users = userService.getAll();
        return Result.returnResult(200,"success",users);
    }


}
